package com.nageoffer.shortlink.admin.config;

import com.p6spy.engine.spy.P6DataSource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import javax.sql.DataSource;

/**
 * @title: P6spyConfig
 * @Author Z先森
 * @Date: 2023/7/6 10:17
 * @Version 1.0
 */
@Configuration
public class P6spyConfig {
    static class P6DataSourceBeanPostProcessor implements BeanPostProcessor, Ordered {

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof DataSource) {
                return new P6DataSource((DataSource) bean);
            }
            return bean;
        }

        @Override
        public int getOrder() {
            return Ordered.LOWEST_PRECEDENCE;
        }
    }

    @Bean
    public P6DataSourceBeanPostProcessor p6DataSource() {
        return new P6DataSourceBeanPostProcessor();
    }
}
